package com.xinqi.modules.course.demonstrate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.xinqi.modules.course.clazz.convert.ClassGroupConvert;
import com.xinqi.modules.course.clazz.domain.ClassGroupEntity;
import com.xinqi.modules.course.clazz.domain.ClassGroupStudentEntity;
import com.xinqi.modules.course.clazz.mapper.ClassGroupMapper;
import com.xinqi.modules.course.clazz.mapper.ClassGroupStudentMapper;
import com.xinqi.modules.course.clazz.service.ClassGroupStudentService;
import com.xinqi.modules.course.course.mapper.CourseMapper;
import com.xinqi.modules.course.demonstrate.bean.ClassDemoDTO;
import com.xinqi.modules.course.demonstrate.service.DemoGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 示范课程
 */
@Service("demoGroupService")
@RequiredArgsConstructor
public class DemoGroupServiceImpl implements DemoGroupService {

    private final CourseMapper courseMapper;

    /** 分组 */
    private final ClassGroupConvert classGroupConvert;
    private final ClassGroupMapper classGroupMapper;
    private final ClassGroupStudentMapper classGroupStudentMapper;
    private final ClassGroupStudentService classGroupStudentService;

    private final IdentifierGenerator identifierGenerator;

    /**
     * 同步班级分组信息
     * @param demoDTO 课程班级信息
     */
    @Override
    public void initGroup(ClassDemoDTO demoDTO) {
        // 查询父分组信息
        List<ClassGroupEntity> plist = this.findGroup(demoDTO.getOldId(), 0L);
        // 涉及到动态修改 子分组ID需 分条插入分组数据
        plist.forEach(item -> {
            ClassGroupEntity group = item;
            // 生成新的父分组信息
            ClassGroupEntity newGroup = classGroupConvert.newEntity(group);
            newGroup.setId(null);
            newGroup.setCourseId(demoDTO.getCourseId());
            newGroup.setClassId(demoDTO.getId());
            classGroupMapper.insert(newGroup);
            // 生成未分组学生信息
            this.initGroupStudent(demoDTO, group.getId(), newGroup.getId());
            // 子分组 学生生成
            this.initGroupSonAll(demoDTO, group.getId(), newGroup.getId());
        });
    }
    /** 分组信息查询添加 */
    private List<ClassGroupEntity> findGroup(Long classId, Long pid) {
        LambdaQueryWrapper<ClassGroupEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ClassGroupEntity::getClassId, classId);
        wrapper.eq(ClassGroupEntity::getPid, pid);
        List<ClassGroupEntity> list = classGroupMapper.selectList(wrapper);
        return list;
    }
    /** 批量添加分组学生 */
    private void initGroupStudent(ClassDemoDTO demoDTO, Long groupId, Long newGroupId) {
        LambdaQueryWrapper<ClassGroupStudentEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ClassGroupStudentEntity::getGroupId, groupId);
        List<ClassGroupStudentEntity> list = classGroupStudentMapper.selectList(wrapper);
        if (Objects.nonNull(list) && list.size() > 0) {
            list.forEach(item -> {
                item.setId(null);
                item.setGroupId(newGroupId);
                item.setCourseId(demoDTO.getCourseId());
                item.setClassId(demoDTO.getId());
            });
            classGroupStudentService.saveAllBatch(list);
        }
    }
    /** 生成子分组和子分组学生信息 */
    private void initGroupSonAll(ClassDemoDTO demoDTO, Long pid, Long newPid) {
        // 查询子分组信息
        List<ClassGroupEntity> sonList = this.findGroup(demoDTO.getOldId(), pid);
        // 涉及到动态修改 子分组ID需 分条插入分组数据
        for (int i = 0; i < sonList.size(); i++) {
            ClassGroupEntity group = sonList.get(i);
            // 生成新的子分组信息
            ClassGroupEntity newGroup = classGroupConvert.newEntity(group);
            newGroup.setId(null);
            newGroup.setPid(newPid);
            newGroup.setCourseId(demoDTO.getCourseId());
            newGroup.setClassId(demoDTO.getId());
            classGroupMapper.insert(newGroup);
            // 生成子分组学生信息
            this.initGroupStudent(demoDTO, group.getId(), newGroup.getId());
        }
    }

    @Override
    public void initGroupSql(ClassDemoDTO demoDTO) {

        StringBuilder sql = new StringBuilder();

        String[] groupIds = new String[] {"1661534859776397313","1661534859780591618","1661534859784785921","1663094350150012929"
            ,"1663094350154207233","1663094350158401538","1663094350162595842","1663094350166790146","1663094350170984449"
            ,"1663104364570906625","1663719954322350082","1663726642010054658","1663731853491494914","1663732107024588802"};
        Map<String, String> groupIdsMap = new HashMap<>();
        for (int i = 0; i < groupIds.length; i++) {
            Long id = identifierGenerator.nextId(null).longValue();
            groupIdsMap.put(groupIds[i], id.toString());
        }
        String groupSql = "INSERT INTO cl_class_group(`id`, `create_by`, `create_time`, `update_by`, `update_time`, `is_delete`, `delete_time`, `course_id`, `class_id`, `pid`, `name`, `type`, `member_num`, `max_group_num`, `group_num`, `last_time`, `number`, `rule`, `sign`) " +
            "VALUES (" + groupIdsMap.get("1661534859776397313") + ", 1645325062446964737, '2023-05-25 08:49:15', 1645325062446964737, '2023-05-29 16:04:12', 0, 0, " + demoDTO.getCourseId() + ", 1661184856792928257, 0, '合作探究学习组', 'SPECIFIED', 2, 2, 2, NULL, NULL, NULL, NULL)," +
            "(" + groupIdsMap.get("1661534859780591618") + ", 1645325062446964737, '2023-05-25 08:49:15', 1645325062446964737, '2023-05-25 08:49:15', 0, 0, " + demoDTO.getCourseId() + ", 1661184856792928257, 1661534859776397313, '1组', 'SPECIFIED', 2, 2, 2, NULL, NULL, NULL, NULL)," +
            "(" + groupIdsMap.get("1661534859784785921") + ", 1645325062446964737, '2023-05-25 08:49:15', 1645325062446964737, '2023-05-25 08:49:15', 0, 0, " + demoDTO.getCourseId() + ", 1661184856792928257, 1661534859776397313, '2组', 'SPECIFIED', 2, 2, 2, NULL, NULL, NULL, NULL)," +
            "(" + groupIdsMap.get("1663094350150012929") + ", 1645325062446964737, '2023-05-29 16:06:07', 1645325062446964737, '2023-05-29 16:06:07', 0, 0, " + demoDTO.getCourseId() + ", 1661184856792928257, 0, '学习互助分组', 'RANDOM', 2, 5, 5, NULL, NULL, NULL, NULL)," +
            "(" + groupIdsMap.get("1663094350154207233") + ", 1645325062446964737, '2023-05-29 16:06:07', 1645325062446964737, '2023-05-29 16:06:07', 0, 0, " + demoDTO.getCourseId() + ", 1661184856792928257, 1663094350150012929, '1组', 'RANDOM', 2, 5, 5, NULL, NULL, NULL, NULL)," +
            "(" + groupIdsMap.get("1663094350158401538") + ", 1645325062446964737, '2023-05-29 16:06:07', 1645325062446964737, '2023-05-29 16:06:07', 0, 0, " + demoDTO.getCourseId() + ", 1661184856792928257, 1663094350150012929, '2组', 'RANDOM', 2, 5, 5, NULL, NULL, NULL, NULL)," +
            "(" + groupIdsMap.get("1663094350162595842") + ", 1645325062446964737, '2023-05-29 16:06:07', 1645325062446964737, '2023-05-29 16:06:07', 0, 0, " + demoDTO.getCourseId() + ", 1661184856792928257, 1663094350150012929, '3组', 'RANDOM', 2, 5, 5, NULL, NULL, NULL, NULL)," +
            "(" + groupIdsMap.get("1663094350166790146") + ", 1645325062446964737, '2023-05-29 16:06:07', 1645325062446964737, '2023-05-29 16:06:07', 0, 0, " + demoDTO.getCourseId() + ", 1661184856792928257, 1663094350150012929, '4组', 'RANDOM', 2, 5, 5, NULL, NULL, NULL, NULL)," +
            "(" + groupIdsMap.get("1663094350170984449") + ", 1645325062446964737, '2023-05-29 16:06:07', 1645325062446964737, '2023-05-29 16:06:07', 0, 0, " + demoDTO.getCourseId() + ", 1661184856792928257, 1663094350150012929, '5组', 'RANDOM', 2, 5, 5, NULL, NULL, NULL, NULL)," +
            "(" + groupIdsMap.get("1663104364570906625") + ", 1645325062446964737, '2023-05-29 16:45:54', 1645325062446964737, '2023-05-29 16:45:54', 0, 0, " + demoDTO.getCourseId() + ", 1661184856792928257, 1661534859776397313, '3组', 'SPECIFIED', 0, 1, 0, NULL, NULL, NULL, NULL)," +
            "(" + groupIdsMap.get("1663719954322350082") + ", 1645325062446964737, '2023-05-31 09:32:02', 1645325062446964737, '2023-05-31 09:32:02', 0, 0, " + demoDTO.getCourseId() + ", 1661184856792928257, 0, '自由合作学习分组', 'FREEDOM', 0, 1, 0, 1685505600000, NULL, '每个小组三到四人，自由组合，组建小组后药互帮互助，共同进步。', NULL)," +
            "(" + groupIdsMap.get("1663726642010054658") + ", 1654693737893134337, '2023-05-31 09:58:37', 1654693737893134337, '2023-05-31 09:58:37', 0, 0, " + demoDTO.getCourseId() + ", 1661184856792928257, 1663719954322350082, '1111组', 'FREEDOM', 0, 1, 0, 1685505600000, NULL, NULL, '1111')," +
            "(" + groupIdsMap.get("1663731853491494914") + ", 1656548738307391489, '2023-05-31 10:19:19', 1656548738307391489, '2023-05-31 10:19:19', 0, 0, " + demoDTO.getCourseId() + ", 1661184856792928257, 1663719954322350082, '2222组', 'FREEDOM', 0, 1, 0, 1685505600000, NULL, NULL, '2222')," +
            "(" + groupIdsMap.get("1663732107024588802") + ", 1661185385489694721, '2023-05-31 10:20:20', 1661185385489694721, '2023-05-31 10:20:20', 0, 0, " + demoDTO.getCourseId() + ", 1661184856792928257, 1663719954322350082, '1234组', 'FREEDOM', 0, 1, 0, 1685505600000, NULL, NULL, '1234');";
        // 更新测评ID
        for (String item : groupIds) {
            String value = groupIdsMap.get(item);
            groupSql = groupSql.replaceAll(item, value);
        }
        // 更新班级ID
        groupSql = groupSql.replaceAll("1661184856792928257", demoDTO.getId().toString());
        sql.append(groupSql);

        String groupStudentSql = "INSERT INTO cl_class_group_student(`id`, `create_by`, `create_time`, `update_by`, `update_time`, `course_id`, `class_id`, `group_id`, `student_id`, `is_group_leader`, `renames`) " +
            "VALUES (" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-25 08:49:25', 1645325062446964737, '2023-05-29 17:12:02', " + demoDTO.getCourseId() + ", 1661184856792928257, 1661534859780591618, 1650458704827445249, 1, '1组')," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-29 16:04:44', 1645325062446964737, '2023-05-29 16:04:44', " + demoDTO.getCourseId() + ", 1661184856792928257, 1661534859780591618, 1651875182495043585, 0, '1组')," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-29 16:04:44', 1645325062446964737, '2023-05-29 16:04:44', " + demoDTO.getCourseId() + ", 1661184856792928257, 1661534859780591618, 1653986821394567169, 0, '1组')," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-25 08:49:25', 1645325062446964737, '2023-05-25 08:49:25', " + demoDTO.getCourseId() + ", 1661184856792928257, 1661534859780591618, 1654693737893134337, 0, '1组')," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-29 16:05:15', 1645325062446964737, '2023-05-29 16:05:15', " + demoDTO.getCourseId() + ", 1661184856792928257, 1661534859784785921, 1655410589569056769, 0, '2组')," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-29 16:05:15', 1645325062446964737, '2023-05-29 17:12:08', " + demoDTO.getCourseId() + ", 1661184856792928257, 1661534859784785921, 1656548738307391489, 1, '2组')," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-29 16:06:07', 1645325062446964737, '2023-05-29 16:06:07', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663094350154207233, 1655410589569056769, 0, NULL)," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-29 16:06:07', 1645325062446964737, '2023-05-29 16:06:07', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663094350158401538, 1650458704827445249, 0, NULL)," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-29 16:06:07', 1645325062446964737, '2023-05-29 16:06:07', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663094350158401538, 1655650372358701057, 0, NULL)," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-29 16:06:07', 1645325062446964737, '2023-05-29 16:06:07', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663094350162595842, 1651875182495043585, 0, NULL)," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-29 16:06:07', 1645325062446964737, '2023-05-29 16:06:07', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663094350162595842, 1656548738307391489, 0, NULL)," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-29 16:06:07', 1645325062446964737, '2023-05-29 16:06:07', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663094350166790146, 1653986821394567169, 0, NULL)," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-29 16:06:07', 1645325062446964737, '2023-05-29 16:06:07', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663094350166790146, 1659403958154625026, 0, NULL)," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-29 16:06:07', 1645325062446964737, '2023-05-29 16:06:07', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663094350170984449, 1654693737893134337, 0, NULL)," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-29 16:06:07', 1645325062446964737, '2023-05-29 16:06:07', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663094350170984449, 1661185385489694721, 0, NULL)," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-29 16:05:15', 1645325062446964737, '2023-05-29 16:46:28', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663104364570906625, 1655650372358701057, 0, '2组')," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-29 16:04:44', 1645325062446964737, '2023-05-29 16:46:16', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663104364570906625, 1659403958154625026, 0, '1组')," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-25 08:49:33', 1645325062446964737, '2023-05-29 17:12:13', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663104364570906625, 1661185385489694721, 1, '2组')," +
            "(" + identifierGenerator.nextId(null) + ", 1650458704827445249, '2023-05-31 10:18:39', 1650458704827445249, '2023-05-31 10:18:39', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663726642010054658, 1650458704827445249, 0, NULL)," +
            "(" + identifierGenerator.nextId(null) + ", 1654693737893134337, '2023-05-31 09:58:37', 1654693737893134337, '2023-05-31 09:58:37', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663726642010054658, 1654693737893134337, 0, NULL)," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-31 10:23:32', 1645325062446964737, '2023-05-31 10:23:32', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663726642010054658, 1655650372358701057, 0, NULL)," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-31 10:23:25', 1645325062446964737, '2023-05-31 10:23:25', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663731853491494914, 1651875182495043585, 0, NULL)," +
            "(" + identifierGenerator.nextId(null) + ", 1656548738307391489, '2023-05-31 10:19:19', 1656548738307391489, '2023-05-31 10:19:19', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663731853491494914, 1656548738307391489, 0, NULL)," +
            "(" + identifierGenerator.nextId(null) + ", 1645325062446964737, '2023-05-31 10:23:25', 1645325062446964737, '2023-05-31 10:23:25', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663731853491494914, 1659403958154625026, 0, NULL)," +
            "(" + identifierGenerator.nextId(null) + ", 1653986821394567169, '2023-05-31 10:20:50', 1653986821394567169, '2023-05-31 10:20:50', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663732107024588802, 1653986821394567169, 0, NULL)," +
            "(" + identifierGenerator.nextId(null) + ", 1661185385489694721, '2023-05-31 10:20:20', 1661185385489694721, '2023-05-31 10:20:20', " + demoDTO.getCourseId() + ", 1661184856792928257, 1663732107024588802, 1661185385489694721, 0, NULL);";
        // 更新测评ID
        for (String item : groupIds) {
            String value = groupIdsMap.get(item);
            groupStudentSql = groupStudentSql.replaceAll(item, value);
        }
        // 更新班级ID
        groupStudentSql = groupStudentSql.replaceAll("1661184856792928257", demoDTO.getId().toString());
        sql.append(groupStudentSql);

        // 执行SQL语句
        courseMapper.insertAllSql(sql.toString());
    }

}
